import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.apache.poi.hwpf.HWPFDocument;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.List;

/**
 * Created by lyq on 2017/11/24.
 *
 * 这个可以
 */

public class WordToHtml {

    //输出HTML文件
    public static void writeFile(String content,String path){
        FileOutputStream fos = null;
        BufferedWriter bw =null;
        org.jsoup.nodes.Document dac = Jsoup.parse(content);
        content = dac.html();
        try{
            File file = new File(path);
            fos = new FileOutputStream(file);
            bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
            bw.write(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
                if (fos != null)
                    fos.close();
            }catch (IOException e) {

            }
        }
    }

    public static void WordToHtmlMethod(String fileName,String outPutFile) throws IOException, ParserConfigurationException, TransformerException {
        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .newDocument());
        wordToHtmlConverter.setPicturesManager(new PicturesManager() {
            public String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) {
                return "test/"+s;
            }
        });
        wordToHtmlConverter.processDocument(wordDocument);

        List pics = wordDocument.getPicturesTable().getAllPictures();
        if(pics != null){
            for (int i=0;i<pics.size();i++){
                Picture pic = (Picture) pics.get(i);
                System.out.println();
                try{
                    pic.writeImageContent(new FileOutputStream("E:/test/"+pic.suggestFullFileName()));

                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }
            }
        }
        Document htmlDocument =  wordToHtmlConverter.getDocument();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(out);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "HTML");
        serializer.transform(domSource,streamResult);
        out.close();
        writeFile(new String(out.toByteArray()),outPutFile);
    }

    public static void main(String[] args) {
        try {
            WordToHtmlMethod("E:\\1.doc","E:\\1.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
